package com.droidblackbox.manager;

import java.io.File;
import java.io.IOException;

import android.os.Environment;
import android.os.StatFs;

public class MemoryManager {

	private static MemoryManager instance = null;

	public static final int ERROR = -1;

	public synchronized static MemoryManager getInstance() {
		if (instance == null) {
			synchronized (MemoryManager.class) {
				if (instance == null) {
					instance = new MemoryManager();
				}
			}
		}
		return instance;
	}

	private MemoryManager() {
	}

	public boolean externalMemoryAvailable() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	public long getAvailableInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long availableBlocks = stat.getAvailableBlocks();
		return availableBlocks * blockSize;
	}

	public long getTotalInternalMemorySize() {
		File path = Environment.getDataDirectory();
		StatFs stat = new StatFs(path.getPath());
		long blockSize = stat.getBlockSize();
		long totalBlocks = stat.getBlockCount();
		return totalBlocks * blockSize;
	}

	public long getAvailableExternalMemorySize() {
		if (externalMemoryAvailable()) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long availableBlocks = stat.getAvailableBlocks();
			return availableBlocks * blockSize;
		} else {
			return ERROR;
		}
	}

	public long getTotalExternalMemorySize() {
		if (externalMemoryAvailable()) {
			File path = Environment.getExternalStorageDirectory();
			StatFs stat = new StatFs(path.getPath());
			long blockSize = stat.getBlockSize();
			long totalBlocks = stat.getBlockCount();
			return totalBlocks * blockSize;
		} else {
			return ERROR;
		}
	}

	public long getUsingAppMemorySize() {
		if (externalMemoryAvailable()) {
			long appMemorySize = 0;

			try {
				File file = FileManager.getFile(FileManager.FOLDER_RECORD);
				File[] files = file.listFiles();

				for (File f : files) {
					appMemorySize += f.length();
				}

			} catch (IOException e) {
			}
			return appMemorySize;
		} else {
			return ERROR;
		}
	}

	public int getUsageInternalMemory() {

		double total;
		double avail;
		double using;
		double result;
		total = MemoryManager.getInstance().getTotalInternalMemorySize();
		avail = MemoryManager.getInstance().getAvailableInternalMemorySize();
		using = total - avail;
		result = (using / total) * 100.0;

		return (int) Math.round(result);
	}

	public int getUsageExternalMemory() {

		double total;
		double avail;
		double using;
		double result;
		total = MemoryManager.getInstance().getTotalExternalMemorySize();
		avail = MemoryManager.getInstance().getAvailableExternalMemorySize();
		using = total - avail;
		result = (using / total) * 100.0;

		return (int) Math.round(result);
	}

	public String formatSize(double size) {
		return formatSize((long) size);
	}

	public String formatSize(long size) {
		String suffix = "KB";
		if (size >= 1024) {
			size /= 1024;
			if (size >= 1024) {
				suffix = "MB";
				size /= 1024;
			}
		}

		StringBuilder resultBuffer = new StringBuilder(Long.toString(size));

		int commaOffset = resultBuffer.length() - 3;
		while (commaOffset > 0) {
			resultBuffer.insert(commaOffset, ',');
			commaOffset -= 3;
		}

		if (suffix != null)
			resultBuffer.append(suffix);
		return resultBuffer.toString();
	}

	/**
	 * Total : total * percent / 100.0
	 * 
	 * @param percent
	 * @param total
	 * @return
	 */
	public double getPercentToValue(long percent, long total) {
		double a = total;
		double b = percent;
		return a * b / 100.0;

	}

	public int getPercent(long data, long total) {
		double a = data;
		double b = total;
		double c = (a / b) * 100.0;
		return (int) Math.round(c);
	}
}
